/*
*  @author 侯新征
*  @time 2021年7月22日22:19:47
* */
package com.zx.controller;

import com.zx.DAO.NewsDao;
import com.zx.entity.News;

import com.zx.service.NewsInte;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;


import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
//@RequestMapping("/NewsController")
public class NewsController {

    @Resource
    public NewsInte newsInte;


    //查看所有新闻
    @RequestMapping("/allnews")
    public String list(ModelMap model){
        List<News> list = newsInte.findNews();
        model.put("list",list);

        System.out.println("访问到了后台控制器f"+list);
        return "newslist";
    }

    //发布新闻
    @RequestMapping("/addnews")
    public String addNews(News news){
        newsInte.addNews(news);
        return "newslist";
    }

    @InitBinder
    public void initData(WebDataBinder wdb){
        wdb.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }



    /*删除新闻*/
    @RequestMapping("/deletenews")
    public String delete(int id){
        newsInte.deleteById(id);
        return "redirect:allnews";
    }

}
